
/****************************************************************************/
/*Copyright (c) 2011, Florent DEVILLE.                                      */
/*All rights reserved.                                                      */
/*                                                                          */
/*Redistribution and use in source and binary forms, with or without        */
/*modification, are permitted provided that the following conditions        */
/*are met:                                                                  */
/*                                                                          */
/* - Redistributions of source code must retain the above copyright         */
/*notice, this list of conditions and the following disclaimer.             */
/* - Redistributions in binary form must reproduce the above                */
/*copyright notice, this list of conditions and the following               */
/*disclaimer in the documentation and/or other materials provided           */
/*with the distribution.                                                    */
/* - The names of its contributors cannot be used to endorse or promote     */
/*products derived from this software without specific prior written        */
/*permission.                                                               */
/* - The source code cannot be used for commercial purposes without         */ 
/*its contributors' permission.                                             */
/*                                                                          */
/*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       */
/*"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT         */
/*LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS         */
/*FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE            */
/*COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,       */
/*INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,      */
/*BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;          */
/*LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER          */
/*CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT        */
/*LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN         */
/*ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE           */
/*POSSIBILITY OF SUCH DAMAGE.                                               */
/****************************************************************************/

#include "GRTMenuRenderRenderPicture.h"

#include <QLineEdit>
#include <QPushButton>
#include <QGroupBox>
#include <QLabel>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QComboBox>

#include "GRTFrmRenderedPicture.h"

#include "GRTPlug.h"
#include "RTMalort.h"
using RT::RTScene;

#include "GRTScene.h"
#include "GRTCameraEye.h"

#include "GRTVisualisation.h"
#include "GRTMemoryLeak.h"


//constructor
GRTMenuRenderRenderPicture::GRTMenuRenderRenderPicture(QWidget* parent) : QWidget(parent), _fRatioX(16), _fRatioY(9),
	_fResolutionX(400), _fResolutionY(225), _fFOV(45), _oversampling(RTRaytracer::NO_OVERSAMPLING),
	_renderingThreadHandle(0)
{

	setWindowTitle("Render Options");

	//camera options
	QLabel* lbRatio = new QLabel("Ratio : ");
	QLabel* lbRatioX = new QLabel("x=");
	QLabel* lbRatioY = new QLabel("y=");
	_leRatioX = new QLineEdit();
	_leRatioY = new QLineEdit();

	QLabel* lbFOV = new QLabel("FOV : ");
	_leFOV = new QLineEdit();

	QGridLayout* cameraLayout = new QGridLayout();
	cameraLayout->addWidget(lbRatio, 0, 0, 1, 1);
	cameraLayout->addWidget(lbRatioX, 1, 2, 1, 1);
	cameraLayout->addWidget(_leRatioX, 1, 3, 1, 1);
	cameraLayout->addWidget(lbRatioY, 2, 2, 1, 1);
	cameraLayout->addWidget(_leRatioY, 2, 3, 1, 1);
	cameraLayout->addWidget(lbFOV, 3, 0, 1, 1);
	cameraLayout->addWidget(_leFOV, 3, 1, 1, 3);
	QGroupBox* groupCamera = new QGroupBox("Camera");
	groupCamera->setLayout(cameraLayout);

	//picture options
	QLabel* lbResolution = new QLabel("Resolution : ");
	QLabel* lbResolutionX = new QLabel("x=");
	QLabel* lbResolutionY = new QLabel("y=");
	_leResolutionX = new QLineEdit();
	_leResolutionY = new QLineEdit();
	QLabel* lbOversampling = new QLabel("Oversampling : ");
	_cbOversampling = new QComboBox();
	_cbOversampling->addItem("No oversampling");
	_cbOversampling->addItem("Oversampling x4");
	_cbOversampling->addItem("Oversampling x16");

	QGridLayout* pictureLayout = new QGridLayout();
	pictureLayout->addWidget(lbResolution, 0, 0, 1, 1);
	pictureLayout->addWidget(lbResolutionX, 1, 2, 1, 1);
	pictureLayout->addWidget(_leResolutionX, 1, 3, 1, 1);
	pictureLayout->addWidget(lbResolutionY, 2, 2, 1, 1);
	pictureLayout->addWidget(_leResolutionY, 2, 3, 1, 1);
	pictureLayout->addWidget(lbOversampling, 3, 0, 1, 1);
	pictureLayout->addWidget(_cbOversampling, 3, 1, 1 ,3);
	QGroupBox* groupPicture = new QGroupBox("Picture");
	groupPicture->setLayout(pictureLayout);

	//button
	_buttonRender = new QPushButton("Render");
	_buttonDiscard = new QPushButton("Discard");
	QHBoxLayout* buttonLayout = new QHBoxLayout();
	buttonLayout->addWidget(_buttonRender);
	buttonLayout->addWidget(_buttonDiscard);
	QGroupBox* groupButton = new QGroupBox("");
	groupButton->setLayout(buttonLayout);

	//frame layout
	QVBoxLayout* frameLayout = new QVBoxLayout();
	frameLayout->addWidget(groupCamera);
	frameLayout->addWidget(groupPicture);
	frameLayout->addWidget(groupButton, Qt::AlignRight);

	setLayout(frameLayout);

	//modal window
	setWindowModality(Qt::ApplicationModal);

	//button render
	connect(_buttonRender, SIGNAL(clicked()), this, SLOT(clickRender()));

	//update options
	connect(_leRatioX, SIGNAL(textEdited(const QString&)), this, SLOT(saveRatio()));
	connect(_leRatioY, SIGNAL(textEdited(const QString&)), this, SLOT(saveRatio()));
	connect(_leFOV, SIGNAL(textEdited(const QString&)), this, SLOT(saveFOV()));
	connect(_leResolutionX, SIGNAL(textEdited(const QString&)), this, SLOT(saveResolution()));
	connect(_leResolutionY, SIGNAL(textEdited(const QString&)), this, SLOT(saveResolution()));
	connect(_cbOversampling, SIGNAL(currentIndexChanged(int)), this, SLOT(saveOversampling(int)));

	//connect the thread colsing system
	//GRTFrmRenderedPicture* frm = GUIRTVISU.getRenderFrame();
	//bool res = connect(frm, SIGNAL(closed()), this, SLOT(closeRenderingThread()));
}

//destructor
GRTMenuRenderRenderPicture::~GRTMenuRenderRenderPicture(){}

//fill and show the frame
void GRTMenuRenderRenderPicture::fillAndShow()
{
	_leRatioX->setText(QString::number(_fRatioX));
	_leRatioY->setText(QString::number(_fRatioY));

	_leResolutionX->setText(QString::number(_fResolutionX));
	_leResolutionY->setText(QString::number(_fResolutionY));

	_leFOV->setText(QString::number(_fFOV));

	show();
}

//handle the click to the render button
void GRTMenuRenderRenderPicture::clickRender()
{
	//plug object
	GRTPlug::plugPrimitives();
	GRTPlug::plugLights();
	GRTPlug::plugGlobalAmbientLight();



	//set the raytracer options
	RAYTRACER.setCoordinateSystem(LEFT_HANDED);
	GRTPlug::plugScreen(_fRatioX, _fRatioY, _fFOV);
	GRTPlug::plugResolution(_fResolutionX, _fResolutionY);
	//p.plugCamera(SCENE.getCamera()->getRTEye(), SCENE.getCamera()->getRTAt(), SCENE.getCamera()->getRTUp());
	GRTPlug::plugCamera(SCENE.getCamera()->getTranslate(), SCENE.getCamera()->getAt(), SCENE.getCamera()->getUp());
	GRTPlug::plugOversampling(_oversampling);


	//initialise the render frame
	GUIRTVISU.setSize(_fResolutionX, _fResolutionY);

	//create the rendering routine
	DWORD dwThreadId = 0;
	_renderingThreadHandle = CreateThread(NULL, //Choose default security
								0, //Default stack size
								(LPTHREAD_START_ROUTINE)&renderRoutine,//Routine to execute
								(LPVOID) GUIRTVISU.getBuffer(), //Thread parameter
								0, //Immediately run the thread
								&dwThreadId //Thread Id
								);

	//display the render frame
	GUIRTVISU.display();	
}


//render routine
int renderRoutine(void* buffer)
{
	RAYTRACER.preDraw();

	//render the picture
	RAYTRACER.draw((char*)buffer, 4);

	RTSCENE.close();
	RAYTRACER.close();

	return 0;
}

//save the ratio
void GRTMenuRenderRenderPicture::saveRatio()
{
	_fRatioX = _leRatioX->text().toFloat();
	_fRatioY = _leRatioY->text().toFloat();
}

//save the field of view
void GRTMenuRenderRenderPicture::saveFOV()
{
	_fFOV = _leFOV->text().toFloat();
}

//save the resolution
void GRTMenuRenderRenderPicture::saveResolution()
{
	_fResolutionX = _leResolutionX->text().toFloat();
	_fResolutionY = _leResolutionY->text().toFloat();
}

//save the oversampling state
void GRTMenuRenderRenderPicture::saveOversampling(int index)
{
	if (index < 0)
		return;

	switch(index)
	{
		case 0:
			_oversampling = RTRaytracer::NO_OVERSAMPLING;
			break;
		case 1:
			_oversampling = RTRaytracer::LVL1_OVERSAMPLING;
			break;
		case 2:
			_oversampling = RTRaytracer::LVL2_OVERSAMPLING;
			break;
		default:
			_oversampling = RTRaytracer::NO_OVERSAMPLING;
			break;

	}
}

